package cn.lh.basic.controller;

import cn.lh.basic.service.IVerifyService;
import cn.lh.basic.util.JsonResult;
import cn.lh.org.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    IVerifyService verifyService;

    @GetMapping("/image/{key}")
    private JsonResult imageVerifyCode(@PathVariable("key") String key){

        try {
            String imageVerifyCode = verifyService.getImgCode(key);
            return JsonResult.set(true, "生成验证码成功").setResultObject(imageVerifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, e.getMessage());
        }
    }

    /**
     *  phone
     *  imageCode
     *  imageCodeKey
     */
    @PostMapping("/smsCode")
    private JsonResult sendSmsCode(@RequestBody Map<String, String> map){
        try {
            verifyService.sendSmsCode(map);
            return JsonResult.set(true, "验证码发送成功");
        } catch (BusinessException e) {
            return JsonResult.set(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "系统错误，请稍后尝试");
        }
    }

    @PostMapping("/binderSmsCode")
    private JsonResult binderSmsCode(@RequestBody Map<String, String> map){
        try {
            verifyService.binderSmsCode(map);
            return JsonResult.set(true, "验证码发送成功");
        } catch (BusinessException e) {
            return JsonResult.set(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.set(false, "系统错误，请稍后尝试");
        }
    }
}
